package com.magic.smartcity.view.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;

import com.magic.smartcity.R;
import com.magic.smartcity.databinding.FragmentMeFeedbackSubmitBinding;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;
import com.magic.smartcity.utils.Constant;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MeFeedbackSubmitFragment extends BaseUserCallBackFragment {

    private FragmentMeFeedbackSubmitBinding mBinding;

    @Inject
    IUserPresenter mPresenter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentMeFeedbackSubmitBinding.inflate(getLayoutInflater());
        setHasOptionsMenu(true);
        mPresenter.registerCallBack(this);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login-user", Context.MODE_PRIVATE);

        return mBinding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

    @Override
    public void feedbackSubmitOk(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
        Navigation.findNavController(getView()).popBackStack();
    }

    @Override
    public void error(String str) {

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.user_feedback_submit,menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==R.id.submit){
            if(Constant.TOKEN!=null) {
                String title = mBinding.title.getText().toString();
                String content = mBinding.content.getText().toString();
                if (title.trim().length() == 0 && content.trim().length() == 0) {
                    Toast.makeText(getContext(), "内容和标题不能为空!", Toast.LENGTH_SHORT).show();
                } else {
                    mPresenter.feedbackSubmit(title, content);
                }
            }
        }
        return super.onOptionsItemSelected(item);
    }
}